package dhz.tools.dialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.ResourceManager;

public class JscssDialog extends TitleAreaDialog {
	
	/**
	 * 压缩JS
	 */
	public static final int TYPE_JS = 1;
	/**
	 * 压缩CSS
	 */
	public static final int TYPE_CSS = 2;
	
	private Text txtMin;
	private Button btnJs;
	private Button btnCss;
	
	private int type;
	private String suffix;
	
	public JscssDialog(Shell parentShell) {
		super(parentShell);
	}
	
	protected Control createDialogArea(Composite parent) {
		setMessage("\u8BF7\u8BBE\u7F6EJS/CSS\u7684\u538B\u7F29\u53C2\u6570\u3002");
		setTitle("\u538B\u7F29\u53C2\u6570\u914D\u7F6E");
		setTitleImage(ResourceManager.getPluginImage("dhz-tools", "icons/jscss-compressor-title.png"));
		
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		GridLayout gl_container = new GridLayout(2, false);
		gl_container.verticalSpacing = 10;
		gl_container.marginWidth = 10;
		gl_container.marginTop = 5;
		container.setLayout(gl_container);
		GridData gd_container = new GridData(GridData.FILL_BOTH);
		gd_container.widthHint = 220;
		container.setLayoutData(gd_container);
		
		Label lblNewLabel = new Label(container, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("\u751F\u6210\u540E\u7F00\uFF1A");
		
		txtMin = new Text(container, SWT.BORDER);
		txtMin.setTextLimit(10);
		txtMin.setToolTipText("\u540E\u7F00\u53EA\u80FD\u4E3A\u6570\u5B57\u3001\u5B57\u6BCD\u53CA\u4E0B\u5212\u7EBF\u7684\u7EC4\u5408\uFF0C\u4E14\u4E0D\u80FD\u4E3A\u7A7A\u3002");
		txtMin.setText("min");
		txtMin.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblNewLabel_1 = new Label(container, SWT.NONE);
		lblNewLabel_1.setText("\u538B\u7F29\u7C7B\u578B\uFF1A");
		
		Composite composite = new Composite(container, SWT.NONE);
		composite.setLayout(new RowLayout(SWT.HORIZONTAL));
		
		btnJs = new Button(composite, SWT.CHECK);
		btnJs.setSelection(true);
		btnJs.setText("JS");
		
		btnCss = new Button(composite, SWT.CHECK);
		btnCss.setText("CSS");
		
		return area;
	}

	protected void createButtonsForButtonBar(Composite parent) {
		Button button = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,true);
		button.setText("\u786E\u5B9A");
		Button button_1 = createButton(parent, IDialogConstants.CANCEL_ID,IDialogConstants.CANCEL_LABEL, false);
		button_1.setText("\u53D6\u6D88");
	}

	protected void okPressed() {
		String name = txtMin.getText().trim();
		if(!name.matches("[\\w]+")){
			MessageDialog.openError(this.getParentShell(),"错误","后缀只能是数字、字母及下划线的组合且不能为空！");
			return;
		}
		if(!btnJs.getSelection() && !btnCss.getSelection()){
			MessageDialog.openError(this.getParentShell(),"错误","请至少选择一个压缩类型！");
			return;
		}
		
		suffix = name;
		if(btnJs.getSelection()){
			type |= TYPE_JS;
		}
		if(btnCss.getSelection()){
			type |= TYPE_CSS;
		}
		super.okPressed();
	}

	@Override
	protected void configureShell(Shell newShell) {
		newShell.setText("JS/CSS压缩");
		super.configureShell(newShell);
	}
	
	protected Point getInitialSize() {
		return new Point(239, 208);
	}

	public int getType() {
		return type;
	}

	public String getSuffix() {
		return suffix;
	}

}
